package service;

import dto.UserDto;
import s.Dept;
import s.Role;
import s.User;
import vo.Result;
import vo.UserVo;


/**
 * 业务层—用户
 *
 * @author 熊诗友
 * @since 2025/5/19 12:50
 */
public abstract class UserService extends BaseService<User, UserDto, UserVo> implements IUserService {

    private final IDeptService deptService;
    private final IRoleService roleService;

    private static UserService singleton;

    private UserService() {
        deptService = DeptService.getInstance();
        roleService = RoleService.getInstance();
        data = new User[DATA_SIZE + 1];
        temp = new UserVo[DATA_SIZE];
    }

    public static IUserService getInstance() {
        if (singleton == null) {
            singleton = new UserService() {
                @Override
                User createEntity(UserDto dto, int id) {
                    return null;
                }
            };
        }
        return singleton;
    }

    @Override
    UserVo convertToVo(User user) {
        String deptName = "";
        Dept dept=deptService.findById(user.deptid);
        if (dept != null){
            deptName = dept.name;
        }
        String roleName = "";
        Role role=roleService.findById(user.roleid);
        if (role != null){
            roleName = role.name;
        }
        return new UserVo(user.getId(), user.username, user.name, user.mobile, user.email, user.deptid,
                deptName, user.roleid, roleName,user.enabled);
    }

    @Override
    User createEntity(UserDto dto, String id) {
        return new User(id,dto.roleId,dto.name,dto.mobile,dto.email,dto.deptId, dto.username);
    }

    @Override
    void updateEntity(UserDto dto, User user) {
        user.username  = String.valueOf(dto.username);
        user.name  = dto.name;
        user.mobile = dto.mobile;
        user.email = String.valueOf(dto.email);
        user.deptid = dto.deptId;
        user.roleid = Integer.parseInt(dto.roleId);
    }

    @Override
    public boolean deptInUse(int deptId) {
        boolean  inUse = false;
        for (User user : data){
            if (user!=null && user.deptid==deptId){
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public boolean roleInUse(int roleId) {
        boolean  inUse = false;
        for (User user : data){
            if (user!=null && user.deptid==roleId){
                inUse = true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public Result create(UserDto dto) {
        if (!deptService.existById(dto.deptId)) {
            return Result.failure("创建失败，不存在指定部门");
        }
        if (!roleService.existById(Integer.parseInt(dto.roleId))) {
            return Result.failure("创建失败，不存在指定角色");
        }
        return super.create(dto);
    }

    @Override
    public Result update(UserDto dto,int id) {
        if (!deptService.existById(dto.deptId)) {
            return Result.failure("创建失败，不存在指定部门");
        }
        if (!roleService.existById(Integer.parseInt(dto.roleId))) {
            return Result.failure("创建失败，不存在指定角色");
        }
        return super.update(dto,id);
    }

    @Override
    public  Result toggle(int id) {
        if(!existById(id)){
            return Result.failure("切换状态失败，不存在该数据");
        }
        data[id].enabled = !data[id].enabled;
        return Result.success(data[id].enabled ? "启用成功" : "禁用成功", null);
    }
}
